package cn.slf.ph.dao.impl;

import cn.slf.ph.dao.ShopDao;
import cn.slf.ph.entity.Shop;
import cn.slf.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class ShopDaoImpl implements ShopDao {
    @Override
    public int insert(Shop shop) {
        String sql = "insert into shop values(?,null,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object [] obj = {
                shop.getShopName(),
                shop.getShopPhone(),
                shop.getShopAddress(),
                shop.getShopDescribe(),
                shop.getShopCommunitySort(),
                shop.getShopOrderCount(),
                shop.getShopStatus(),
                new Date(),
                shop.getSalesVolume(),
                shop.getShopProvince(),
                shop.getShopCity(),
                shop.getShopDistrict(),
                shop.getShopLatitude(),
                shop.getShopLongitude(),
                shop.getShopImage(),
                shop.getShopAccount(),
                shop.getShopPassword(),
                shop.getShopGoodsList(),
                shop.getShopCategoryList(),
                shop.getConcernStatus(),
                shop.getDistance(),
                shop.getShopSort(),
                shop.getShopUserId(),
                shop.getCommunityId()
        };
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public int delete(int shopId) {
        String sql = "delete from shop where shopId=?";
        Object [] obj = {shopId};
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public int update(Shop shop) {
        String sql = "update shop set shopName=?,shopPhone=?,shopProvince=?,shopCity=?,shopDistrict=?,shopAddress=?,shopDescribe=?,shopLatitude=?,shopLongitude=? where shopId=?";
        Object [] obj = {
                shop.getShopName(),
                shop.getShopPhone(),
                shop.getShopProvince(),
                shop.getShopCity(),
                shop.getShopDistrict(),
                shop.getShopAddress(),
                shop.getShopDescribe(),
                shop.getShopLatitude(),
                shop.getShopLongitude(),
                shop.getShopId()
        };
        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public List<Shop> selectAll(Map<String, String> map) {
        List<Shop> list = new ArrayList<>();
        // 动态sql
        String sql = "select * from shop where 1=1 ";
        List<Object> params = new ArrayList<>();
        // 判断如果map集合中有shopName则该sql需要进行模糊查询
        String shopName = map.get("shopName");
        if (shopName!=null && !"".equals(shopName)){
            sql += " and shopName like ?";
            params.add("%"+shopName+"%");
        }
        // 判断是否需要进行手机号查询
        String shopPhone = map.get("shopPhone");
        if (shopPhone!=null && !"".equals(shopPhone)){
            sql += " and shopPhone like ?";
            params.add("%"+shopPhone+"%");
        }
        // 判断是否需要进行日期范围查询
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime!=null && !"".equals(startTime)){
            sql += " and addTime between ? and ? ";
            params.add(startTime);
            params.add(endTime);
        }
        // 倒叙展示
        sql += " order by shopId desc ";
        // 判断是否分页
        String pn = map.get("pageNum");
        String ps = map.get("pageSize");
        if (pn!=null && !"".equals(pn)){
            sql += "limit ?,?";
            int pageNum = Integer.parseInt(pn);
            int pageSize = Integer.parseInt(ps);
            params.add((pageNum-1)*pageSize);
            params.add(pageSize);
        }
        // 组成对象
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if (!res.next()) break;
                Shop shop = new Shop(
                        res.getString(1),
                        res.getInt(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getString(6),
                        res.getInt(7),
                        res.getInt(8),
                        res.getDate(9),
                        res.getInt(10),
                        res.getString(11),
                        res.getString(12),
                        res.getString(13),
                        res.getString(14),
                        res.getString(15),
                        res.getString(16),
                        res.getString(17),
                        res.getString(18),
                        res.getString(19),
                        res.getString(20),
                        res.getString(21),
                        res.getString(22),
                        res.getString(23),
                        res.getInt(24),
                        res.getInt(25)
                );
                list.add(shop);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int count = 0;
        // 动态sql
        String sql = "select count(1) from shop where 1=1 ";
        List<Object> params = new ArrayList<>();
        // 判断如果map集合中有shopName则该sql需要进行模糊查询
        String shopName = map.get("shopName");
        if (shopName!=null && !"".equals(shopName)){
            sql += " and shopName like ?";
            params.add("%"+shopName+"%");
        }
        // 判断是否需要进行手机号查询
        String shopPhone = map.get("shopPhone");
        if (shopPhone!=null && !"".equals(shopPhone)){
            sql += " and shopPhone like ?";
            params.add("%"+shopPhone+"%");
        }
        // 判断是否需要进行日期范围查询
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime!=null && !"".equals(startTime)){
            sql += " and addTime between ? and ? ";
            params.add(startTime);
            params.add(endTime);
        }
        // 组成对象
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if (!res.next()) break;
                count = res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return count;
    }
}
